/*
 * KebCore.java
 */

package net.kernub.kebcore.client;

import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import net.kernub.kebcore.client.TestObject;

import net.kernub.kebcore.client.Service;
import net.kernub.kebcore.client.Service.Callback;
import net.kernub.kebcore.client.datatype.CBoolean;

public class KebCore implements EntryPoint
{
	public KebCore() {}

	public void onModuleLoad()
	{
		Backend.create();

		final Label testLabel = new Label("nothing...");
		final Button testButton = new Button( "Press me to test me!" );


		testButton.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				/*
				String url = GWT.getHostPageBaseURL() + "backend/testpost.php";
				Backend.inst().call( url, "toto=WITH_POST", new BackendCallback() {
					public void onReturn( boolean hasError, JavaScriptObject obj )
					{
						Error.add( "TESTPOST.PHP [WITH_POST] return" );
					}
				});

				url = GWT.getHostPageBaseURL() + "backend/testpost.php?toto=WITH_GET";
				Backend.inst().call( url, null, new BackendCallback() {
					public void onReturn( boolean hasError, JavaScriptObject obj )
					{
						Error.add( "TESTPOST.PHP [WITH_GET] return" );
					}
				});
				*/

				Service.call( "/core/Visitor/test", "NO_1", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (1: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(1) received answer! " + value.en() + " ... " + value.fr() );
					}
				});
				/*
				Service.call( "/core/Visitor/test", "NO_2", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (2: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(2) received answer! " + value.en() + " ... " + value.fr() );
					}
				});
				Service.call( "/core/Visitor/test", "NO_3", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (3: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(3) received answer! " + value.en() + " ... " + value.fr() );
					}
				});
				*/

				//Backend.inst().cancelCalls();
			}
		});

		final VerticalPanel vPanel = new VerticalPanel();
		vPanel.add( testLabel );
		vPanel.add( testButton );

		RootPanel.get().add( vPanel );

	}
};

/*
public class KebCore implements EntryPoint {

  //private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

  public void onModuleLoad() {
	
		Backend.create();

		final Label testLabel = new Label("nothing...");
		final Button testButton = new Button( "Press me to test me!" );

		testButton.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				String url = GWT.getHostPageBaseURL() + "backend/testpost.php";
				Backend.inst().call( url, "toto=WITH_POST", new BackendCallback() {
					public void onReturn( boolean hasError, JavaScriptObject obj )
					{
						Error.add( "TESTPOST.PHP [WITH_POST] return" );
					}
				});

				url = GWT.getHostPageBaseURL() + "backend/testpost.php?toto=WITH_GET";
				Backend.inst().call( url, null, new BackendCallback() {
					public void onReturn( boolean hasError, JavaScriptObject obj )
					{
						Error.add( "TESTPOST.PHP [WITH_GET] return" );
					}
				});

				Service.call( "/core/Visitor/test", "NO_1", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (1: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(1) received answer! " + value.en() + " ... " + value.fr() );
					}
				});
				Service.call( "/core/Visitor/test", "NO_2", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (2: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(2) received answer! " + value.en() + " ... " + value.fr() );
					}
				});
				Service.call( "/core/Visitor/test", "NO_3", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (3: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(3) received answer! " + value.en() + " ... " + value.fr() );
					}
				});

				//Backend.inst().cancelCalls();

				Service.call( "/core/Visitor/test", "NO_4", new Service.Callback<TestObject>() {
					public void onReturn( TestObject value )
					{
						testLabel.setText( testLabel.getText() + " (4: " + value.now() + "[" + value.test() + "]" + ") " );
						//testLabel.setText( "(3) received answer! " + value.en() + " ... " + value.fr() );
					}
				});

				DeferredCommand.addCommand( new Command() {
					public void execute()
					{
						Service.call( "/core/Visitor/test", "NO_5", new Service.Callback<TestObject>() {
							public void onReturn( TestObject value )
							{
								testLabel.setText( testLabel.getText() + " (5: " + value.now() + "[" + value.test() + "]" + ") " );
								//testLabel.setText( "(3) received answer! " + value.en() + " ... " + value.fr() );
							}
						});
					}
				});

			}
		});

		final VerticalPanel vPanel = new VerticalPanel();
		vPanel.add( testLabel );
		vPanel.add( testButton );

		RootPanel.get().add( vPanel );
	}
};
*/

		/*

    final Button sendButton = new Button("Send");
    final TextBox nameField = new TextBox();
    nameField.setText("GWT User");

    // We can add style names to widgets
    sendButton.addStyleName("sendButton");

    // Add the nameField and sendButton to the RootPanel
    // Use RootPanel.get() to get the entire body element
    RootPanel.get("nameFieldContainer").add(nameField);
    RootPanel.get("sendButtonContainer").add(sendButton);

    // Focus the cursor on the name field when the app loads
    nameField.setFocus(true);
    nameField.selectAll();

    // Create the popup dialog box
    final DialogBox dialogBox = new DialogBox();
    dialogBox.setText("Remote Procedure Call");
    dialogBox.setAnimationEnabled(true);
    final Button closeButton = new Button("Close");
    // We can set the id of a widget by accessing its Element
    closeButton.getElement().setId("closeButton");
    final Label textToServerLabel = new Label();
    final HTML serverResponseLabel = new HTML();
    VerticalPanel dialogVPanel = new VerticalPanel();
    dialogVPanel.addStyleName("dialogVPanel");
    dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
    dialogVPanel.add(textToServerLabel);
    dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
    dialogVPanel.add(serverResponseLabel);
    dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
    dialogVPanel.add(closeButton);
    dialogBox.setWidget(dialogVPanel);

    // Add a handler to close the DialogBox
    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        dialogBox.hide();
        sendButton.setEnabled(true);
        sendButton.setFocus(true);
      }
    });

    // Create a handler for the sendButton and nameField
    class MyHandler implements ClickHandler, KeyUpHandler {
       // * Fired when the user clicks on the sendButton.
      public void onClick(ClickEvent event) {
        sendNameToServer();
      }

       // * Fired when the user types in the nameField.
      public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
          sendNameToServer();
        }
      }

       // * Send the name from the nameField to the server and wait for a response.
      private void sendNameToServer() {
        sendButton.setEnabled(false);
        String textToServer = nameField.getText();
        textToServerLabel.setText(textToServer);
        serverResponseLabel.setText("");
        greetingService.greetServer(textToServer, new AsyncCallback<String>() {
          public void onFailure(Throwable caught) {
            // Show the RPC error message to the user
            dialogBox.setText("Remote Procedure Call - Failure");
            serverResponseLabel.addStyleName("serverResponseLabelError");
            serverResponseLabel.setHTML(SERVER_ERROR);
            dialogBox.center();
            closeButton.setFocus(true);
          }

          public void onSuccess(String result) {
            dialogBox.setText("Remote Procedure Call");
            serverResponseLabel.removeStyleName("serverResponseLabelError");
            serverResponseLabel.setHTML(result);
            dialogBox.center();
            closeButton.setFocus(true);
          }
        });
      }
    }

    // Add a handler to send the name to the server
    MyHandler handler = new MyHandler();
    sendButton.addClickHandler(handler);
    nameField.addKeyUpHandler(handler);
		*/
